package com.xiaodong.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xiaodong.reggie.currency.BaseContest;
import com.xiaodong.reggie.currency.R;
import com.xiaodong.reggie.entity.ShoppingCart;
import com.xiaodong.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartContorller {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //添加购物车
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {

//        log.info("购物车=={}",shoppingCart);
        boolean ma = true;
        ShoppingCart one = null;
        LambdaQueryWrapper<ShoppingCart> queryWrapper = null;
        LambdaUpdateWrapper<ShoppingCart> wrapper = null;

        //先根据dishId或setmealId查询购物车里的数据
        if (shoppingCart.getDishId() != null) {
            queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            queryWrapper.eq(ShoppingCart::getUserId,BaseContest.getId());
            one = shoppingCartService.getOne(queryWrapper);

            if (one == null) {
                shoppingCart.setUserId(BaseContest.getId());
                shoppingCart.setNumber(1);
                ma = shoppingCartService.save(shoppingCart);
                if (ma) {
                    return R.success("添加成功");
                }
                return R.error("添加失败");
            }

            wrapper = new LambdaUpdateWrapper();
            wrapper.eq(ShoppingCart::getDishId, one.getDishId());
            wrapper.eq(ShoppingCart::getUserId,BaseContest.getId());
            wrapper.set(ShoppingCart::getNumber, one.getNumber() + 1);
            ma = shoppingCartService.update(wrapper);
            if (ma) {
                return R.success("添加成功");
            }
            return R.error("添加失败");
        }
        if (shoppingCart.getSetmealId() != null) {
            queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            queryWrapper.eq(ShoppingCart::getUserId,BaseContest.getId());
            one = shoppingCartService.getOne(queryWrapper);

            if (one == null) {
                shoppingCart.setUserId(BaseContest.getId());
                shoppingCart.setNumber(1);
                ma = shoppingCartService.save(shoppingCart);
                if (ma) {
                    return R.success("添加成功");
                }
                return R.error("添加失败");
            }

            wrapper = new LambdaUpdateWrapper();
            wrapper.eq(ShoppingCart::getSetmealId, one.getSetmealId());
            wrapper.eq(ShoppingCart::getUserId,BaseContest.getId());
            wrapper.set(ShoppingCart::getNumber, one.getNumber() + 1);
            ma = shoppingCartService.update(wrapper);
            if (ma) {
                return R.success("添加成功");
            }
            return R.error("添加失败");
        }
        return R.error("没有发现该菜品或套餐");
    }

    //移除购物车
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {

        boolean ma = true;
        ShoppingCart one = null;
        LambdaQueryWrapper<ShoppingCart> queryWrapper = null;
        LambdaUpdateWrapper<ShoppingCart> wrapper = null;
        //先根据dishId或setmealId查询购物车里的数据
        if (shoppingCart.getDishId() != null) {
            queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            queryWrapper.eq(ShoppingCart::getUserId,BaseContest.getId());
            one = shoppingCartService.getOne(queryWrapper);

            //如果number等于1就直接删除
            if (one.getNumber() == 1) {

                queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(ShoppingCart::getUserId, BaseContest.getId());
                ma = shoppingCartService.remove(queryWrapper);
                if (ma) {
                    return R.success("移除成功");
                }
                return R.error("移除失败");
            }

            //根据dishId修改购物车里的Number数据
            wrapper = new LambdaUpdateWrapper();
            wrapper.eq(ShoppingCart::getDishId, one.getDishId());
            wrapper.eq(ShoppingCart::getUserId,BaseContest.getId());
            wrapper.set(ShoppingCart::getNumber, one.getNumber() - 1);
            ma = shoppingCartService.update(wrapper);
            if (ma) {
                return R.success("移除成功");
            }
            return R.error("移除失败");
        }
        if (shoppingCart.getSetmealId() != null) {
            queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            queryWrapper.eq(ShoppingCart::getUserId,BaseContest.getId());
            one = shoppingCartService.getOne(queryWrapper);

            //如果number等于1就直接删除
            if (one.getNumber() == 1) {

                queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(ShoppingCart::getUserId, BaseContest.getId());
                ma = shoppingCartService.remove(queryWrapper);
                if (ma) {
                    return R.success("移除成功");
                }
                return R.error("移除失败");
            }

            //根据dishId修改购物车里的Number数据
            wrapper = new LambdaUpdateWrapper();
            wrapper.eq(ShoppingCart::getSetmealId, one.getSetmealId());
            wrapper.eq(ShoppingCart::getUserId,BaseContest.getId());
            wrapper.set(ShoppingCart::getNumber, one.getNumber() - 1);
            ma = shoppingCartService.update(wrapper);
            if (ma) {
                return R.success("移除成功");
            }
            return R.error("移除失败");
        }
        return R.error("没有发现该菜品或套餐");

    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {

        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContest.getId());

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        if (list != null) {
            return R.success(list);
        }

        return R.error("没收据");
    }

    @DeleteMapping("/clean")
    public R<String> clean() {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContest.getId());
        shoppingCartService.remove(queryWrapper);

        return R.success("删除成功");

    }
}
